package com.itheima.day08_api;

public class Demo2_String {
    public static void main(String[] args) {
        //目标：快速熟悉string提供的处理字符串的常用方法
        //1.length()：获取字符串的长度
        String s1 = "hello";
        System.out.println("s1.length() = " + s1.length());
        //2.charAt(index)：获取指定索引处的字符
        char c = s1.charAt(0);
        System.out.println("c = " + c);
        //
        for (int i = 0; i < s1.length(); i++) {
            char ch = s1.charAt(i);
            System.out.println(ch);
        }
        //3.toCharArray()：将字符串转换成字符数组
        char[] chars = s1.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char ch = chars[i];
            System.out.println(ch);
        }
        //4.equals(obj)：比较两个字符串是否相等, 比较的是内容,== 比较的是地址值
        String s2 = new String("黑马");
        String s3 = new String("黑马");
        System.out.println("s2 == s3 = " + s2 == s3);
        System.out.println("s2.equals(s3) = " + s2.equals(s3));
        //5.equalsIgnoreCase(obj)：比较两个字符串是否相等,忽略大小写
        //equals()方法比较字符串内容是否相同
        String c1 = "34aefg";
        String c2 = "34AEFG";
        System.out.println("c1.equals(c2) = " + c1.equals(c2));
        System.out.println("c1.equalsIgnoreCase(c2) = " + c1.equalsIgnoreCase(c2));
        //6.substring(start,end)：截取字符串，包含start，不包含end
        String s4 = "java是世界上最好的编程语言";
        String rs = s4.substring(0, 8);
        System.out.println("rs = " + rs);
        //7.substring(start)：截取字符串，包含start，截取到末尾
        String s5 = "java是世界上最好的编程语言";
        String rs2 = s5.substring(9);
        System.out.println("rs2 = " + rs2);
        //8.replace(old,new)：替换字符串，将字符串old替换成new
        String info = "这个电影简直是垃圾， 垃圾电影";
        String rs3 = info.replace("垃圾", "**");
        System.out.println("rs3 = " + rs3);
        //9.contains(obj)：判断字符串中是否包含obj
        String info2 = "java是世界上最好的编程语言之一，我爱java,Java爱我";
        System.out.println("info2.contains(\"java\") = " + info2.contains("java"));
        System.out.println("info2.contains(\"JAVA\") = " + info2.contains("JAVA"));
        System.out.println("info2.contains(\"java2\") = " + info2.contains("java2"));
        //10.startsWith(prefix)：判断字符串是否以prefix开头, 返回boolean,
        // startsWith()方法判断字符串是否以prefix开头
        //endsWith()方法判断字符串是否以prefix结尾
        String rs4 = "张三丰";
        rs4.startsWith("张");
        rs4.endsWith("丰");
        rs4.endsWith("2");
        //11.split(regex)：按照regex将字符串分割成多个部分，返回字符串数组
        String rs5 = "张无忌,周芷若,赵敏,刘冲";
        String[] names = rs5.split(",");
        for (int i = 0; i < names.length; i++) {
            System.out.println("names[" + i + "] = " + names[i]);
        }
    }
}
